package com.dhcc.DRGsHosp.facade.impl;

import java.io.File;
import java.io.FileOutputStream;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.inject.Named;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;

import org.dayatang.domain.InstanceFactory;
import org.dayatang.querychannel.QueryChannelService;
import org.dayatang.utils.Page;
import org.openkoala.koala.commons.InvokeResult;
import org.openkoala.koalacommons.resourceloader.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.xml.sax.helpers.AttributesImpl;

import com.dhcc.DRGsHosp.application.MedicalRecordApplication;
import com.dhcc.DRGsHosp.core.domain.MedicalRecord;
import com.dhcc.DRGsHosp.facade.MedicalRecordFacade;
import com.dhcc.DRGsHosp.facade.dto.IndexCalDTO;
import com.dhcc.DRGsHosp.facade.dto.MedicalRecordDTO;
import com.dhcc.DRGsHosp.facade.impl.assembler.IndexCalAssembler;
import com.dhcc.DRGsHosp.facade.impl.assembler.MedicalRecordAssembler;
import com.dhcc.DRGsHosp.infra.util.DataSourceUtil;
@Named("drgsHospFacadeImpl29")
public class MedicalRecordFacadeImpl implements MedicalRecordFacade {
	SimpleDateFormat  dateFormater= new SimpleDateFormat("yyyy-MM-dd");//对日期格式进行转换
	@Autowired
	private MedicalRecordApplication  application;
	
	
	private QueryChannelService queryChannel;

    private QueryChannelService getQueryChannelService(){
       if(queryChannel==null){
          queryChannel = InstanceFactory.getInstance(QueryChannelService.class,"queryChannel");
       }
     return queryChannel;
    }
	
	public InvokeResult getMedicalRecord(String id) {
		return InvokeResult.success(MedicalRecordAssembler.toDTO(application.getMedicalRecord(id)));
	}
	
	public InvokeResult creatMedicalRecord(MedicalRecordDTO medicalRecordDTO) {
		application.creatMedicalRecord(MedicalRecordAssembler.toEntity(medicalRecordDTO));
		return InvokeResult.success();
	}
	
	public InvokeResult updateMedicalRecord(MedicalRecordDTO medicalRecordDTO) {
		application.updateMedicalRecord(MedicalRecordAssembler.toEntity(medicalRecordDTO));
		return InvokeResult.success();
	}
	
	public InvokeResult removeMedicalRecord(String id) {
		application.removeMedicalRecord(application.getMedicalRecord(id));
		return InvokeResult.success();
	}
	
	public InvokeResult removeMedicalRecords(String[] ids) {
		Set<MedicalRecord> medicalRecords= new HashSet<MedicalRecord>();
		for (String id : ids) {
			medicalRecords.add(application.getMedicalRecord(id));
		}
		application.removeMedicalRecords(medicalRecords);
		return InvokeResult.success();
	}
	
	public List<MedicalRecordDTO> findAllMedicalRecord() {
		return MedicalRecordAssembler.toDTOs(application.findAllMedicalRecord());
	}
	
	public Page<MedicalRecordDTO> pageQueryMedicalRecord(MedicalRecordDTO queryVo, int currentPage, int pageSize) {
		List<Object> conditionVals = new ArrayList<Object>();
	   	StringBuilder jpql = new StringBuilder("select _medicalRecord from MedicalRecord _medicalRecord   where 1=1 ");
	   	if (queryVo.getId() != null && !"".equals(queryVo.getId())) {
	   		jpql.append(" and _medicalRecord.id like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getId()));
	   	}		
	   	if (queryVo.getRecordId() != null && !"".equals(queryVo.getRecordId())) {
	   		jpql.append(" and _medicalRecord.recordId like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getRecordId()));
	   	}		
	   	if (queryVo.getPatientId() != null && !"".equals(queryVo.getPatientId())) {
	   		jpql.append(" and _medicalRecord.patientId like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getPatientId()));
	   	}		
	   	if (queryVo.getMrNo() != null && !"".equals(queryVo.getMrNo())) {
	   		jpql.append(" and _medicalRecord.mrNo like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getMrNo()));
	   	}		
	   	if (queryVo.getPatientName() != null && !"".equals(queryVo.getPatientName())) {
	   		jpql.append(" and _medicalRecord.patientName like ?");
	   		conditionVals.add(MessageFormat.format("% {0}%", queryVo.getPatientName()));
	   	}		
	   	if (queryVo.getSex() != null && !"".equals(queryVo.getSex())) {
	   		jpql.append(" and _medicalRecord.sex like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getSex()));
	   	}		
	   	if (queryVo.getAgeYear() != null) {
	   		jpql.append(" and _medicalRecord.ageYear=?");
	   		conditionVals.add(queryVo.getAgeYear());
	   	}	
	   	if (queryVo.getAgeMonth() != null) {
	   		jpql.append(" and _medicalRecord.ageMonth=?");
	   		conditionVals.add(queryVo.getAgeMonth());
	   	}	
	   	if (queryVo.getAgeDays() != null) {
	   		jpql.append(" and _medicalRecord.ageDays=?");
	   		conditionVals.add(queryVo.getAgeDays());
	   	}	
	   	if (queryVo.getMarriage() != null && !"".equals(queryVo.getMarriage())) {
	   		jpql.append(" and _medicalRecord.marriage like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getMarriage()));
	   	}		
	   	if (queryVo.getDayOfbirth() != null) {
	   		jpql.append(" and _medicalRecord.dayOfbirth between ? and ? ");
	   		conditionVals.add(queryVo.getDayOfbirth());
	   		conditionVals.add(queryVo.getDayOfbirthEnd());
	   	}	
	   	if (queryVo.getInDate() != null) {
	   		jpql.append(" and _medicalRecord.inDate between ? and ? ");
	   		conditionVals.add(queryVo.getInDate());
	   		conditionVals.add(queryVo.getInDateEnd());
	   	}	
	   	if (queryVo.getInDepcode() != null && !"".equals(queryVo.getInDepcode())) {
	   		jpql.append(" and _medicalRecord.inDepcode like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getInDepcode()));
	   	}		
	   	if (queryVo.getInDep() != null && !"".equals(queryVo.getInDep())) {
	   		jpql.append(" and _medicalRecord.inDep like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getInDep()));
	   	}		
	   	if (queryVo.getInWardcode() != null && !"".equals(queryVo.getInWardcode())) {
	   		jpql.append(" and _medicalRecord.inWardcode like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getInWardcode()));
	   	}		
	   	if (queryVo.getInWard() != null && !"".equals(queryVo.getInWard())) {
	   		jpql.append(" and _medicalRecord.inWard like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getInWard()));
	   	}		
	   	if (queryVo.getOutWay() != null && !"".equals(queryVo.getOutWay())) {
	   		jpql.append(" and _medicalRecord.outWay like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getOutWay()));
	   	}		
	   	if (queryVo.getInWay() != null && !"".equals(queryVo.getInWay())) {
	   		jpql.append(" and _medicalRecord.inWay like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getInWay()));
	   	}		
	   	if (queryVo.getIpCount() != null) {
	   		jpql.append(" and _medicalRecord.ipCount=?");
	   		conditionVals.add(queryVo.getIpCount());
	   	}	
	   	if (queryVo.getOpDiagnosiscode() != null && !"".equals(queryVo.getOpDiagnosiscode())) {
	   		jpql.append(" and _medicalRecord.opDiagnosiscode like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getOpDiagnosiscode()));
	   	}		
	   	if (queryVo.getOpDiagnosis() != null && !"".equals(queryVo.getOpDiagnosis())) {
	   		jpql.append(" and _medicalRecord.opDiagnosis like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getOpDiagnosis()));
	   	}		
	   	if (queryVo.getOutDate() != null) {
	   		jpql.append(" and _medicalRecord.outDate between ? and ? ");
	   		conditionVals.add(queryVo.getOutDate());
	   		conditionVals.add(queryVo.getOutDateEnd());
	   	}	
	   	if (queryVo.getOutDepcode() != null && !"".equals(queryVo.getOutDepcode())) {
	   		jpql.append(" and _medicalRecord.outDepcode like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getOutDepcode()));
	   	}		
	   	if (queryVo.getOutDep() != null && !"".equals(queryVo.getOutDep())) {
	   		jpql.append(" and _medicalRecord.outDep like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getOutDep()));
	   	}		
	   	if (queryVo.getDepMastercode() != null && !"".equals(queryVo.getDepMastercode())) {
	   		jpql.append(" and _medicalRecord.depMastercode like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getDepMastercode()));
	   	}		
	   	if (queryVo.getDepMaster() != null && !"".equals(queryVo.getDepMaster())) {
	   		jpql.append(" and _medicalRecord.depMaster like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getDepMaster()));
	   	}		
	   	if (queryVo.getProtomedicusCode() != null && !"".equals(queryVo.getProtomedicusCode())) {
	   		jpql.append(" and _medicalRecord.protomedicusCode like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getProtomedicusCode()));
	   	}		
	   	if (queryVo.getProtomedicus() != null && !"".equals(queryVo.getProtomedicus())) {
	   		jpql.append(" and _medicalRecord.protomedicus like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getProtomedicus()));
	   	}		
	   	if (queryVo.getPhysicianCode() != null && !"".equals(queryVo.getPhysicianCode())) {
	   		jpql.append(" and _medicalRecord.physicianCode like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getPhysicianCode()));
	   	}		
	   	if (queryVo.getPhysician() != null && !"".equals(queryVo.getPhysician())) {
	   		jpql.append(" and _medicalRecord.physician like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getPhysician()));
	   	}		
	   	if (queryVo.getResidentDoctorcode() != null && !"".equals(queryVo.getResidentDoctorcode())) {
	   		jpql.append(" and _medicalRecord.residentDoctorcode like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getResidentDoctorcode()));
	   	}		
	   	if (queryVo.getResidentDoctor() != null && !"".equals(queryVo.getResidentDoctor())) {
	   		jpql.append(" and _medicalRecord.residentDoctor like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getResidentDoctor()));
	   	}		
	   	if (queryVo.getPhysicianLearncode() != null && !"".equals(queryVo.getPhysicianLearncode())) {
	   		jpql.append(" and _medicalRecord.physicianLearncode like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getPhysicianLearncode()));
	   	}		
	   	if (queryVo.getPhysicianLearn() != null && !"".equals(queryVo.getPhysicianLearn())) {
	   		jpql.append(" and _medicalRecord.physicianLearn like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getPhysicianLearn()));
	   	}		
	   	if (queryVo.getInternCode() != null && !"".equals(queryVo.getInternCode())) {
	   		jpql.append(" and _medicalRecord.internCode like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getInternCode()));
	   	}		
	   	if (queryVo.getIntern() != null && !"".equals(queryVo.getIntern())) {
	   		jpql.append(" and _medicalRecord.intern like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getIntern()));
	   	}		
	   	if (queryVo.getNurseCode() != null && !"".equals(queryVo.getNurseCode())) {
	   		jpql.append(" and _medicalRecord.nurseCode like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getNurseCode()));
	   	}		
	   	if (queryVo.getNurse() != null && !"".equals(queryVo.getNurse())) {
	   		jpql.append(" and _medicalRecord.nurse like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getNurse()));
	   	}		
	   	if (queryVo.getPayment() != null && !"".equals(queryVo.getPayment())) {
	   		jpql.append(" and _medicalRecord.payment like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getPayment()));
	   	}		
	   	if (queryVo.getCostamount() != null) {
	   		jpql.append(" and _medicalRecord.costamount=?");
	   		conditionVals.add(queryVo.getCostamount());
	   	}	
	   	if (queryVo.getCostself() != null) {
	   		jpql.append(" and _medicalRecord.costself=?");
	   		conditionVals.add(queryVo.getCostself());
	   	}	
	   	if (queryVo.getDangerlevel() != null && !"".equals(queryVo.getDangerlevel())) {
	   		jpql.append(" and _medicalRecord.dangerlevel like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getDangerlevel()));
	   	}		
	   	if (queryVo.getDrugcost() != null) {
	   		jpql.append(" and _medicalRecord.drugcost=?");
	   		conditionVals.add(queryVo.getDrugcost());
	   	}	
	   	if (queryVo.getPargroupName() != null && !"".equals(queryVo.getPargroupName())) {
	   		jpql.append(" and _medicalRecord.pargroupName like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getPargroupName()));
	   	}		
	   	if (queryVo.getDiagnoseCodemain() != null && !"".equals(queryVo.getDiagnoseCodemain())) {
	   		jpql.append(" and _medicalRecord.diagnoseCodemain like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getDiagnoseCodemain()));
	   	}		
	   	if (queryVo.getDiagnoseDessmain() != null && !"".equals(queryVo.getDiagnoseDessmain())) {
	   		jpql.append(" and _medicalRecord.diagnoseDessmain like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getDiagnoseDessmain()));
	   	}		
	   	if (queryVo.getNewbornWeight() != null) {
	   		jpql.append(" and _medicalRecord.newbornWeight=?");
	   		conditionVals.add(queryVo.getNewbornWeight());
	   	}	
	   	if (queryVo.getNewbornDays() != null) {
	   		jpql.append(" and _medicalRecord.newbornDays=?");
	   		conditionVals.add(queryVo.getNewbornDays());
	   	}	
	   	if (queryVo.getVentilatorHour() != null) {
	   		jpql.append(" and _medicalRecord.ventilatorHour=?");
	   		conditionVals.add(queryVo.getVentilatorHour());
	   	}	
	   	if (queryVo.getInDays() != null) {
	   		jpql.append(" and _medicalRecord.inDays=?");
	   		conditionVals.add(queryVo.getInDays());
	   	}	
	   	if (queryVo.getMaterialcost() != null) {
	   		jpql.append(" and _medicalRecord.materialcost=?");
	   		conditionVals.add(queryVo.getMaterialcost());
	   	}	
        Page<MedicalRecord> pages = getQueryChannelService()
		   .createJpqlQuery(jpql.toString())
		   .setParameters(conditionVals)
		   .setPage(currentPage, pageSize)
		   .pagedList();
		   
        return new Page<MedicalRecordDTO>(pages.getStart(), pages.getResultCount(),pageSize, MedicalRecordAssembler.toDTOs(pages.getData()));
	}
	
	@Override
	public InvokeResult findCaseList(IndexCalDTO dto,Boolean isAllUsers,int page, int pageSize,int sEcho) {/*
		//dto.setStdYear(drgsIndexCalFacade.getStdID(dto.getDateFrom(),dto.getDateTo()));
		String quotaManageYear = drgsIndexCalFacade.getManageYear(dto);
		dto.setManageYear(quotaManageYear);
		List<Map<String,Object>> listMap = new LinkedList<Map<String,Object>>();
		Page<Object[]> medicalRecordsPage = application.findMedicalRecord(page,pageSize,IndexCalAssembler.toEntity(dto),isAllUsers);
		List<Object[]> medicalRecords = medicalRecordsPage.getData();
//		int pageSize = medicalRecordsPage.getPageSize();//每页可容纳的记录数量
		long totalCount = medicalRecordsPage.getResultCount();//符合查询条件的记录总数
		for(Object[] mr:medicalRecords){
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("mrNo", mr[0]);
			map.put("patientName",mr[1]);
			map.put("sex", mr[2]);
			map.put("ageYear", mr[3]);
			map.put("outDate",  mr[4]);
			map.put("outDep", mr[5]);
			map.put("inDays", mr[7]);
			map.put("diagnoseDessmain", mr[8]);
			map.put("mdc", mr[11]);
			map.put("adrg", mr[10]);
			map.put("drg", mr[9]);
			map.put("id", mr[12]);
			map.put("drgcode", mr[13]);
			map.put("inDate",  mr[14]);
			map.put("costamount", mr[15]);
			map.put("drugcost", mr[16]);
			map.put("weight", mr[17]);
			map.put("avgDaysStd", mr[18]);
			map.put("avgFeeStd", mr[19]);
//			Object obj = application.getDRGsStdBy(mr[13]!=null?mr[13].toString():"", quotaManageYear, "AvgDays", dto.getpType());
//			map.put("avgDaysStd", obj!=null?obj.toString():"");
//			obj = application.getDRGsStdBy(mr[13]!=null?mr[13].toString():"", quotaManageYear, "AvgFee", dto.getpType());
//			map.put("avgFeeStd", obj!=null?obj.toString():"");
			listMap.add(map);
		}
		Map<String,Object> maps = new HashMap<String, Object>();
		maps.put("sEcho",sEcho);
		maps.put("iTotalRecords", totalCount);
		maps.put("iTotalDisplayRecords", totalCount);
		maps.put("aaData", listMap);
		
		return InvokeResult.success(maps);
	*/
		return InvokeResult.success();
	}

	/* (non-Javadoc)
	 * @see com.dhcc.DRGsHosp.facade.MedicalRecordFacade#getMedicalRecordNum(java.lang.String)
	 */
	@Override
	public List<Map<String, Object>> getMedicalRecordNum(String year,String hospCode) {
		return application.getMedicalRecordNum(year,hospCode);
	}
	
	@Override
	public InvokeResult dataExport(IndexCalDTO dto){
		if(!StringUtils.hasLength(dto.getDateFrom()) || !StringUtils.hasLength(dto.getDateTo())){
			Date dNow = new Date();   //当前时间
			Date dBefore = new Date();
			 
			Calendar calendar = Calendar.getInstance(); //得到日历
			calendar.setTime(dNow);//把当前时间赋给日历
			calendar.add(Calendar.DAY_OF_MONTH, -1);  //设置为前一天
			dBefore = calendar.getTime();   //得到前一天的时间

			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); //设置时间格式
			String dateFrom = sdf.format(dBefore);    //格式化前一天
			String dateTo = sdf.format(dNow); //格式化当前时间
			dto.setDateFrom(dateFrom);
			dto.setDateTo(dateTo);
		}
		String dataPath=DataSourceUtil.class.getResource("/").getFile().toString();  
		dataPath = dataPath.substring(1,dataPath.lastIndexOf("WEB-INF"))+"upload/";
		if (!new File(dataPath).exists()) {
			new File(dataPath).mkdirs();
		}
		String filename = dto.getDateFrom()+"~"+dto.getDateTo()+".xml";
		try{
			List<Object[]> resultList = application.findMedicalRecords(IndexCalAssembler.toEntity(dto));
			if(resultList.size()>20000){
				throw new Exception("数据量超过20000, 请缩减区间！");
			}
			
			Result resultXml = new StreamResult(new FileOutputStream(dataPath+filename)); //输出到new.xml  
	        SAXTransformerFactory sff = (SAXTransformerFactory)SAXTransformerFactory.newInstance();  
	        TransformerHandler th = sff.newTransformerHandler();  
	        th.setResult(resultXml);  
	          
	        Transformer transformer = th.getTransformer();  
	        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); //编码格式是UTF-8  
	        transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //换行  
	        th.startDocument(); //开始xml文档  
	        AttributesImpl attr = new AttributesImpl();  
	        
	        th.startElement("", "", "CASES", attr);
			for(Object[] objs: resultList){
				th.startElement("", "", "CASE", attr);
				
				th.startElement("", "", "MRADM", attr);
	        	th.characters((objs[0]!=null?objs[0].toString():" ").toCharArray(), 0, (objs[0]!=null?objs[0].toString():" ").length());
	        	th.endElement("", "", "MRADM");
//	        	th.startElement("", "", "HOSPCODE", attr);
//	        	th.characters("".toCharArray(), 0, ("").length());
//	        	th.endElement("", "", "HOSPCODE");
//	        	th.startElement("", "", "HOSPNAME", attr);
//	        	th.characters((objs[2]!=null?objs[2].toString():" ").toCharArray(), 0, (objs[2]!=null?objs[2].toString():" ").length());
//	        	th.endElement("", "", "HOSPNAME");
//	        	th.startElement("", "", "PAYMENTCOODE", attr);
//	        	th.characters((objs[3]!=null?objs[3].toString():" ").toCharArray(), 0, (objs[3]!=null?objs[3].toString():" ").length());
//	        	th.endElement("", "", "PAYMENTCOODE");
	        	th.startElement("", "", "PAYMENT", attr);
	        	th.characters((objs[38]!=null?objs[38].toString():" ").toCharArray(), 0, (objs[38]!=null?objs[38].toString():" ").length());
	        	th.endElement("", "", "PAYMENT");
	        	th.startElement("", "", "NAME", attr);
	        	th.characters((objs[4]!=null?objs[4].toString():" ").toCharArray(), 0, (objs[4]!=null?objs[4].toString():" ").length());
	        	th.endElement("", "", "NAME");
	        	th.startElement("", "", "GENDER", attr);
	        	th.characters((objs[5]!=null?objs[5].toString():" ").toCharArray(), 0, (objs[5]!=null?objs[5].toString():" ").length());
	        	th.endElement("", "", "GENDER");
	        	th.startElement("", "", "BIRTH", attr);
	        	th.characters((objs[10]!=null?objs[10].toString():" ").toCharArray(), 0, (objs[10]!=null?objs[10].toString():" ").length());
	        	th.endElement("", "", "BIRTH");
	        	th.startElement("", "", "AGEYEAR", attr);
	        	th.characters((objs[6]!=null?objs[6].toString():" ").toCharArray(), 0, (objs[6]!=null?objs[6].toString():" ").length());
	        	th.endElement("", "", "AGEYEAR");
	        	th.startElement("", "", "AGEMONTH", attr);
	        	th.characters((objs[7]!=null?objs[7].toString():" ").toCharArray(), 0, (objs[7]!=null?objs[7].toString():" ").length());
	        	th.endElement("", "", "AGEMONTH");
	        	th.startElement("", "", "NEWBORNDAYS", attr);
	        	th.characters((objs[47]!=null?objs[47].toString():" ").toCharArray(), 0, (objs[47]!=null?objs[47].toString():" ").length());
	        	th.endElement("", "", "NEWBORNDAYS");
	        	th.startElement("", "", "NEWBORNWEIGHT", attr);
	        	th.characters((objs[46]!=null?objs[46].toString():" ").toCharArray(), 0, (objs[46]!=null?objs[46].toString():" ").length());
	        	th.endElement("", "", "NEWBORNWEIGHT");
	        	th.startElement("", "", "VENTILATOR", attr);
	        	th.characters((objs[48]!=null?objs[48].toString():" ").toCharArray(), 0, (objs[48]!=null?objs[48].toString():" ").length());
	        	th.endElement("", "", "VENTILATOR");
	        	th.startElement("", "", "INWAY", attr);
	        	th.characters((objs[17]!=null?objs[17].toString():" ").toCharArray(), 0, (objs[17]!=null?objs[17].toString():" ").length());
	        	th.endElement("", "", "INWAY");
//	        	th.startElement("", "", "TREATTYPE", attr);
//	        	th.characters((objs[14]!=null?objs[14].toString():" ").toCharArray(), 0, (objs[14]!=null?objs[14].toString():" ").length());
//	        	th.endElement("", "", "TREATTYPE");
	        	th.startElement("", "", "INDATE", attr);
	        	th.characters((objs[11]!=null?objs[11].toString():" ").toCharArray(), 0, (objs[11]!=null?objs[11].toString():" ").length());
	        	th.endElement("", "", "INDATE");
	        	th.startElement("", "", "INDEP", attr);
	        	th.characters((objs[13]!=null?objs[13].toString():" ").toCharArray(), 0, (objs[13]!=null?objs[13].toString():" ").length());
	        	th.endElement("", "", "INDEP");
	        	th.startElement("", "", "INWARD", attr);
	        	th.characters((objs[15]!=null?objs[15].toString():" ").toCharArray(), 0, (objs[15]!=null?objs[15].toString():" ").length());
	        	th.endElement("", "", "INWARD");
//	        	th.startElement("", "", "TRANSDEP", attr);
//	        	th.characters((objs[18]!=null?objs[18].toString():" ").toCharArray(), 0, (objs[18]!=null?objs[18].toString():" ").length());
//	        	th.endElement("", "", "TRANSDEP");
	        	th.startElement("", "", "OUTDEP", attr);
	        	th.characters((objs[23]!=null?objs[23].toString():" ").toCharArray(), 0, (objs[23]!=null?objs[23].toString():" ").length());
	        	th.endElement("", "", "OUTDEP");
//	        	th.startElement("", "", "WTMARK", attr);
//	        	th.characters((objs[20]!=null?objs[20].toString():" ").toCharArray(), 0, (objs[20]!=null?objs[20].toString():" ").length());
//	        	th.endElement("", "", "WTMARK");
//	        	th.startElement("", "", "MARRIAGE", attr);
//	        	th.characters((objs[21]!=null?objs[21].toString():" ").toCharArray(), 0, (objs[21]!=null?objs[21].toString():" ").length());
//	        	th.endElement("", "", "MARRIAGE");
	        	th.startElement("", "", "INDAYS", attr);
	        	th.characters((objs[49]!=null?objs[49].toString():" ").toCharArray(), 0, (objs[49]!=null?objs[49].toString():" ").length());
	        	th.endElement("", "", "INDAYS");
	        	th.startElement("", "", "OUTWAY", attr);
	        	th.characters((objs[16]!=null?objs[16].toString():" ").toCharArray(), 0, (objs[16]!=null?objs[16].toString():" ").length());
	        	th.endElement("", "", "OUTWAY");
	        	th.startElement("", "", "OUTDATE", attr);
	        	th.characters((objs[21]!=null?objs[21].toString():" ").toCharArray(), 0, (objs[21]!=null?objs[21].toString():" ").length());
	        	th.endElement("", "", "OUTDATE");
//	        	th.startElement("", "", "CLINICALPATHWAY", attr);
//	        	th.characters((objs[25]!=null?objs[25].toString():" ").toCharArray(), 0, (objs[25]!=null?objs[25].toString():" ").length());
//	        	th.endElement("", "", "CLINICALPATHWAY");
//	        	th.startElement("", "", "OPDIAG", attr);
//	        	th.characters((objs[26]!=null?objs[26].toString():" ").toCharArray(), 0, (objs[26]!=null?objs[26].toString():" ").length());
//	        	th.endElement("", "", "OPDIAG");
//	        	th.startElement("", "", "IPDIAG", attr);
//	        	th.characters((objs[27]!=null?objs[27].toString():" ").toCharArray(), 0, (objs[27]!=null?objs[27].toString():" ").length());
//	        	th.endElement("", "", "IPDIAG");
	        	th.startElement("", "", "DEPMASTER", attr);
	        	th.characters((objs[25]!=null?objs[25].toString():" ").toCharArray(), 0, (objs[25]!=null?objs[25].toString():" ").length());
	        	th.endElement("", "", "DEPMASTER");
//	        	th.startElement("", "", "PROTOMEDICUS", attr);
//	        	th.characters((objs[29]!=null?objs[29].toString():" ").toCharArray(), 0, (objs[29]!=null?objs[29].toString():" ").length());
//	        	th.endElement("", "", "PROTOMEDICUS");
	        	th.startElement("", "", "PHYSICIAN", attr);
	        	th.characters((objs[29]!=null?objs[29].toString():" ").toCharArray(), 0, (objs[29]!=null?objs[29].toString():" ").length());
	        	th.endElement("", "", "PHYSICIAN");
	        	th.startElement("", "", "RESIDENT", attr);
	        	th.characters((objs[31]!=null?objs[31].toString():" ").toCharArray(), 0, (objs[31]!=null?objs[31].toString():" ").length());
	        	th.endElement("", "", "RESIDENT");
	        	th.startElement("", "", "NURSE", attr);
	        	th.characters((objs[37]!=null?objs[37].toString():" ").toCharArray(), 0, (objs[37]!=null?objs[37].toString():" ").length());
	        	th.endElement("", "", "NURSE");
	        	th.startElement("", "", "PHYSICIANLEARN", attr);
	        	th.characters((objs[33]!=null?objs[33].toString():" ").toCharArray(), 0, (objs[33]!=null?objs[33].toString():" ").length());
	        	th.endElement("", "", "PHYSICIANLEARN");
	        	
	        	
	        	
	        	String parid = objs[61]!=null?objs[61].toString():"";
	        	th.startElement("", "", "DIAGNOSISES", attr);
	        	//诊断
	        	List<Object[]> diagList = application.findDiagnosisBy(parid);
				if(diagList.size()>0){
					for(Object[] diagObjs: diagList){
						th.startElement("", "", "DIAGNOSIS", attr);
						th.startElement("", "", "DIAGID", attr);
			        	th.characters((diagObjs[0]!=null?diagObjs[0].toString():" ").toCharArray(), 0, (diagObjs[0]!=null?diagObjs[0].toString():" ").length());
			        	th.endElement("", "", "DIAGID");
						th.startElement("", "", "DIAGCODE", attr);
			        	th.characters((diagObjs[1]!=null?diagObjs[1].toString():" ").toCharArray(), 0, (diagObjs[1]!=null?diagObjs[1].toString():" ").length());
			        	th.endElement("", "", "DIAGCODE");
			        	th.startElement("", "", "DIAGDESC", attr);
			        	th.characters((diagObjs[2]!=null?diagObjs[2].toString():" ").toCharArray(), 0, (diagObjs[2]!=null?diagObjs[2].toString():" ").length());
			        	th.endElement("", "", "DIAGDESC");
			        	
			        	th.endElement("", "", "DIAGNOSIS");
					}
				}else{
					th.characters((" ").toCharArray(), 0, (" ").length());
				}
				th.endElement("", "", "DIAGNOSISES");
				//手术
				th.startElement("", "", "OPERATIONS", attr);
				List<Object[]> operList = application.findOperationBy(parid);
				if(operList.size()>0){
					for(Object[] operObjs: operList){
						th.startElement("", "", "OPERATION", attr);
						th.startElement("", "", "OPERID", attr);
			        	th.characters((operObjs[0]!=null?operObjs[0].toString():" ").toCharArray(), 0, (operObjs[0]!=null?operObjs[0].toString():" ").length());
			        	th.endElement("", "", "OPERID");
						th.startElement("", "", "OPERCODE", attr);
			        	th.characters((operObjs[1]!=null?operObjs[1].toString():" ").toCharArray(), 0, (operObjs[1]!=null?operObjs[1].toString():" ").length());
			        	th.endElement("", "", "OPERCODE");
			        	th.startElement("", "", "OPERDESC", attr);
			        	th.characters((operObjs[2]!=null?operObjs[2].toString():" ").toCharArray(), 0, (operObjs[2]!=null?operObjs[2].toString():" ").length());
			        	th.endElement("", "", "OPERDESC");
			        	
			        	th.endElement("", "", "OPERATION");
					}
				}else{
					th.characters((" ").toCharArray(), 0, (" ").length());
				}
				th.endElement("", "", "OPERATIONS");
				th.endElement("", "", "CASE");
			}
			th.endElement("", "", "CASES");
			th.endDocument();
		}catch(Exception e){
			return InvokeResult.failure(e.getMessage());
		}
		System.out.println("导出文件位置:"+dataPath+filename);
		return InvokeResult.success("upload/"+filename);
	}

	/* (non-Javadoc)
	 * @see com.dhcc.DRGsHosp.facade.MedicalRecordFacade#getLastDate()
	 */
	@Override
	public String getLatestDate() {
		return application.getLatestDate();
	}
	/**
	 * 
	 *  @Description：获取某年每月的医保病例数
	 *  @Author ZouYiyu  
	 *  @see com.dhcc.DRGsHosp.facade.MedicalRecordFacade#getMedicalRecordYbNum(java.lang.String)
	 */
	@Override
	public List<Map<String, Object>> getMedicalRecordYbNum(String year,String hospCode) {
		return application.getMedicalRecordYbNum(year,hospCode);
	}
	@Override
	public InvokeResult getAllWard() {
		List<Object[]> wardLists = application.getAllWard();
		return InvokeResult.success(wardLists);
	} 		

}
